package com.example.hello;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

public class MyImage extends AppCompatImageView {

    private int mRadius;

    public MyImage(@NonNull Context context) {
        super(context);
    }

    public MyImage(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyImage(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 获取宽高最小值
        int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
        // 计算半径值
        mRadius = size / 2;
        setMeasuredDimension(size, size);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // 所涉及类的关系Canvas->圆心、半径和Paint->BitmapShader->Bitmap和Matrix
        // 获取图片资源
        Drawable drawable = getDrawable();

        if (null != drawable) {
            // 设置圆心
            int cx = mRadius;
            int cy = mRadius;
            // 将drawable对象转成位图Bitmap对象
            Bitmap bitmap = getDrableBitmap(drawable);
            // 通过位图创建着色器，后两个参数表示x轴和y轴的平铺方式
            BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            // 计算缩放比例值
            float mScale = (mRadius * 2.0f) / Math.min(bitmap.getHeight(), bitmap.getWidth());
            // 创建矩阵对象及设置缩放比例
            Matrix matrix = new Matrix();
            matrix.setScale(mScale, mScale);
            // 设置着色器缩放比例
            bitmapShader.setLocalMatrix(matrix);

            // 创建画笔对象及设置着色器
            Paint paint = new Paint();
            paint.setShader(bitmapShader);

            // 通过canvas对象绘图
            canvas.drawCircle(cx, cy, mRadius, paint);
        } else {
            super.onDraw(canvas);
        }
    }

    private Bitmap getDrableBitmap(Drawable drawable) {
        // 判断drawable是否是BitmapDrawable对象，是就直接返回位图
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bd = (BitmapDrawable) drawable;
            return bd.getBitmap();
        }
        // 获取图片宽高
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        // 创建位图对象
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

        // 通过位图对象创建画布对象
        Canvas canvas = new Canvas(bitmap);

        // 设置图片边界
        drawable.setBounds(0, 0, w, h);
        // 设置画布
        drawable.draw(canvas);

        return bitmap;

    }

}